package ui.history;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import tool.DateChooserJButton;
import tool.MyButton;
import tool.MyLabel;
import vo.UserVo;
import vo.BusinessConditionVo;
import blservice.history.经营情况BLService;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import client.boot.BLServiceManager;

import javax.swing.JScrollPane;

import java.awt.Color;

public class BusinessConditionPanel extends JPanel{
	
	private static final long serialVersionUID = 6661483539001152774L;
	
	@SuppressWarnings("unused")
	private UserVo user;
	
	private 经营情况BLService bl;
	private DateChooserJButton from;
	private JTable table;

	public BusinessConditionPanel(UserVo user){
		this.user=user;
		try {
			bl=BLServiceManager.get经营情况BLService(null);
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e1) {
			JOptionPane.showMessageDialog(this,e1.getMessage());
			e1.printStackTrace();
			return;
		}
		setLayout(null);
		this.setOpaque(false);
		setSize(646,433);
		
		JLabel label = new JLabel("\u7ECF\u8425\u60C5\u51B5\u8868");
		label.setForeground(Color.WHITE);
		label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label.setBounds(275, 15, 97, 19);
		add(label);
		
		from = new tool.DateChooserJButton();
		from.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		from.setBounds(415,55,100,23);
		add(from);
		
		JButton button = new MyButton("Graphics/Button73-23.png");
		button.setText("查询");
		button.addActionListener(e->showFrom());
		button.setBounds(526, 55, 73, 23);
		add(button);
		
		JLabel lblFrom = new MyLabel("起始日期");
		lblFrom.setBounds(336, 55, 57, 23);
		add(lblFrom);
		
		table = new JTable(){
			
			private static final long serialVersionUID = 8999208543419910360L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			};
		};
		
		JScrollPane jsp=new JScrollPane(table);
		jsp.setBounds(47, 96, 552, 284);
		add(jsp);
		
		JButton button_1 = new MyButton("Graphics/Button93-23.png");
		button_1.setText("导出");
		button_1.addActionListener(new tool.JTableExportListener(table, "经营情况", from.getText()));
		button_1.setBounds(506, 390, 93, 23);
		add(button_1);		
		
		showAll();
		
	}
	
	void showAll(){
		BusinessConditionVo result;
		try {
			result = bl.get经营情况();
			display(result);
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
		
	}
	
	private void showFrom(){
		BusinessConditionVo from=new BusinessConditionVo();
		from.set日期(this.from.getDate().getTime());
		BusinessConditionVo result;
		try {
			result = bl.get经营情况(from);
			display(result);
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
		
	}

	private void display(BusinessConditionVo result) {
		if(result.isSuccessed()){
			table.setModel(new DefaultTableModel(new String[][]{
					{"折让后总收入:",tool.LongStringTransformer.LongToString(result.get折让后总收入())},
					{"折让",tool.LongStringTransformer.LongToString(result.get折让())},
					{"销售收入",tool.LongStringTransformer.LongToString(result.get销售收入())},
					{"商品类收入:",tool.LongStringTransformer.LongToString(result.get商品类收入())},					
					{"商品报溢收入",tool.LongStringTransformer.LongToString(result.get商品报溢收入())},
					{"成本调价收入",tool.LongStringTransformer.LongToString(result.get成本调价收入())},
					{"进货退货差价收入",tool.LongStringTransformer.LongToString(result.get进货退货差价收入())},
					{"代金券与实际收款差额收入",tool.LongStringTransformer.LongToString(result.get代金券与实际收款差额收入())},
					{"总支出:",tool.LongStringTransformer.LongToString(result.get总支出())},
					{"销售成本:",tool.LongStringTransformer.LongToString(result.get销售成本())},
					{"商品类支出:",tool.LongStringTransformer.LongToString(result.get商品类支出())},
					{"商品报损支出",tool.LongStringTransformer.LongToString(result.get商品报损支出())},
					{"商品赠出支出",tool.LongStringTransformer.LongToString(result.get商品赠出支出())},
					{"利润",tool.LongStringTransformer.LongToString(result.get利润())}
			}, new String[]{
					"类型","金额"
			}));
		}else{
			JOptionPane.showMessageDialog(this,"获取经营情况失败\n"+result.getFailMessage());
		}
	}
}
